package lu.med.praxiswahl.wahlumed.client.utilities;

import java.io.File;
import java.util.ArrayList;

import javax.swing.JTable;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * 
 * @author Lars Holger Roth, Patrick Kendzo
 */
public class ColumnWidthControler {
	
	private ArrayList<Integer> column = new ArrayList<Integer>();

	private XMLReader reader;
	private String widthConfigTag;
	private String tableColumnConfigTag;
	
	public ColumnWidthControler(String configFile, String configTag) {

		File file = new File(configFile);
		reader = XMLReader.getWidthConfigXMLReaderInstance(file);
		this.widthConfigTag = configTag;

		Node tagNode = reader.getDoc().getElementsByTagName(widthConfigTag).item(0);
		
		this.fillOutColumnArray(tagNode);
	}
	
	public ColumnWidthControler(String configFile, String configTag, JTable table) {

		File file = new File(configFile);
		reader = XMLReader.getTableConfigXMLReaderInstance(file);
		this.tableColumnConfigTag = configTag;

		Node tagNode = reader.getDoc().getElementsByTagName(tableColumnConfigTag).item(0);

		this.fillOutColumnArray(tagNode);
	}
	

	public void fillOutColumnArray(Node tagNode) {
		//column.clear();
		NodeList tagChilds = tagNode.getChildNodes();
		for (int i = 0; i < tagChilds.getLength(); i++) {
			Node node = tagChilds.item(i);

			if (node.getNodeType() != Node.ELEMENT_NODE) {
				if (!node.getNodeValue().trim().isEmpty()) {
					column.add(new Integer(Integer.parseInt(node.getNodeValue()
							.trim())));
				}

			} else if (node.getNodeType() == Node.ELEMENT_NODE) {
				this.fillOutColumnArray(node);
			}

		}
	}

	public int getColumnWidth(int col) {
		if (column.size() <= col) {
			return 0;
		}
		return column.get(col);
	}

	public ArrayList<Integer> getColumn() {
		return column;
	}

	public void setColumn(ArrayList<Integer> column) {
		this.column = column;
	}

	public XMLReader getReader() {
		return reader;
	}

	public void setReader(XMLReader reader) {
		this.reader = reader;
	}

	public String getWidthConfigTag() {
		return widthConfigTag;
	}

	public String getTableColumnConfigTag() {
		return tableColumnConfigTag;
	}
	
	
	
}
